package com.rightsidetech.http.controller.admin.mall;

import com.github.pagehelper.PageInfo;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.rightsidetech.commom.base.DelReq;
import com.rightsidetech.commom.base.ResponseData;
import com.rightsidetech.commom.enums.ModifyName;
import com.rightsidetech.http.aspect.Auth;
import com.rightsidetech.http.aspect.Log;
import com.rightsidetech.http.aspect.MallLog;
import com.rightsidetech.http.aspect.Token;
import com.rightsidetech.http.base.BaseController;
import com.rightsidetech.mall.dto.log.CommodityMarketName;
import com.rightsidetech.mall.dto.request.*;
import com.rightsidetech.mall.dto.response.IntegralDetailResp;
import com.rightsidetech.mall.dto.response.IntegralPageResp;
import com.rightsidetech.mall.service.MallIntegralCommodityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shenyu.client.springmvc.annotation.ShenyuSpringMvcClient;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/admin/mall_integral/")
@ShenyuSpringMvcClient(path = "/admin/mall_integral/**")
@Api(tags = "Admin: 积分商城相关")
public class MallIntegralCommodityController extends BaseController {

    @Resource
    private MallIntegralCommodityService mallIntegralCommodityService;

    @Log
    @Auth(bizType = Auth.OPERATOR)
    @Token
    @PostMapping("/save")
    @ApiOperation("添加积分商品")
    @MallLog(modifyType = ModifyName.SAVE,marketName = CommodityMarketName.INTEGRAL_MALL)
    public ResponseData save(@RequestBody IntegralSaveReq req) throws Exception {
        Long loginInfoId = Long.parseLong(getAttribute());
        String loginName = getLoginName();
        if(null == loginInfoId || null == loginName){
            return ResponseData.fail("未找到登录人");
        }
        return mallIntegralCommodityService.saveIntegral(req,loginName,loginInfoId);
    }

    @Log
    @Auth(bizType = Auth.OPERATOR)
    @Token
    @PostMapping("/update")
    @ApiOperation("修改")
    @MallLog(modifyType = ModifyName.UPDATE,marketName = CommodityMarketName.INTEGRAL_MALL)
    public ResponseData updateIntegral(@RequestBody IntegralUpReq req) throws Exception {
        Long loginInfoId = Long.parseLong(getAttribute());
        String loginName = getLoginName();
        if(null == loginInfoId || null == loginName){
            return ResponseData.fail("未找到登录人");
        }
        return mallIntegralCommodityService.updateIntegral(req, loginName, loginInfoId);
    }

    @Log
    @Auth(bizType = Auth.OPERATOR)
    @Token
    @GetMapping("/page")
    @ApiOperation("分页查询")
    public ResponseData<PageInfo<IntegralPageResp>> page(IntegralPageReq req){
         return mallIntegralCommodityService.page(req);
    }

    @Log
    @Auth(bizType = Auth.OPERATOR)
    @Token
    @GetMapping("/list")
    @ApiOperation("不分页列表")
    @ApiOperationSupport(ignoreParameters = {"pageNo", "pageSize"})
    public ResponseData<List<IntegralPageResp>> list(IntegralPageReq req){
        return mallIntegralCommodityService.selectList(req);
    }

    @Log
    @Auth(bizType = Auth.OPERATOR)
    @Token
    @PostMapping("/arraignment")
    @ApiOperation("提审积分商城商品")
    public ResponseData arraignment(@RequestBody DelReq req){
        return mallIntegralCommodityService.arraignment(req);
    }

    @Log
    @Auth(bizType = Auth.OPERATOR)
    @Token
    @PostMapping("/verify")
    @ApiOperation("审核积分商品")
    public ResponseData verify(@RequestBody VerifyCommodityReq req) throws Exception {
        Long loginInfoId = Long.parseLong(getAttribute());
        String loginName = getLoginName();
        if(null == loginInfoId || null == loginName){
            return ResponseData.fail("未找到登录人");
        }
        return mallIntegralCommodityService.verify(req,loginName,loginInfoId);
    }

    @Log
    @Auth(bizType = Auth.OPERATOR)
    @Token
    @PostMapping("/grounding")
    @ApiOperation("上下架积分商品")
    @MallLog(modifyType = ModifyName.GROUNDING,marketName = CommodityMarketName.INTEGRAL_MALL)
    public ResponseData grounding(@RequestBody CommodityIsGroundingReq req){
        return mallIntegralCommodityService.grounding(req);
    }

    @Log
    @Auth(bizType = Auth.OPERATOR)
    @Token
    @PostMapping("/sort")
    @ApiOperation("积分商品排序")
    public ResponseData sort(@RequestBody CommoditySortReq req){
       return mallIntegralCommodityService.sort(req);
    }


    @Log
    @Auth(bizType = Auth.OPERATOR)
    @Token
    @GetMapping("/detail")
    @ApiOperation("积分商品详情")
    public ResponseData<IntegralDetailResp> findDetail(Long id){
         return mallIntegralCommodityService.detail(id);
    }

    @Log
    @Auth(bizType = Auth.OPERATOR)
    @Token
    @PostMapping("/popular")
    @ApiOperation("设置爆款积分商品")
    @MallLog(modifyType = ModifyName.POPULAR,marketName = CommodityMarketName.INTEGRAL_MALL)
    public ResponseData isPopular(@RequestBody CommodityPopularReq req){
        return mallIntegralCommodityService.popular(req);
    }
}
